﻿using WhutDoomCheck.Server.Configs;
using WhutDoomCheck.Server.Services;

namespace WhutDoomCheck.Server.Extentions
{
    public static class TokenFactoryExtension
    {
        public static IServiceCollection AddTokenFactory(this IServiceCollection services, Action<TokenFactoryConfiguration> options)
        {
            var config = new TokenFactoryConfiguration();
            options(config);
            if (config.Audience is null) throw new Exception(nameof(config.Audience));
            if (config.Issuer is null) throw new Exception(nameof(config.Issuer));
            if (config.SigningKey is null) throw new Exception(nameof(config.SigningKey));

            services.AddScoped(services =>
            {
                return new AccessTokenService(config);
            });

            return services;
        }
    }
}
